package com.wg.web_app.service;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.JsonElement;
import com.wg.web_app.dao.SuperDaoFactory;
import com.wg.web_app.dao.table.TableGU;
import com.wg.web_app.dao.table.TableGroup;
import com.wg.web_app.dao.table.TableUN;
import com.wg.web_app.domin.DataBuilder;
import com.wg.web_app.domin.response.*;

public class GroupService {
	
	int APPLICATION_STATUS_APPLY = 0;
	int APPLICATION_STATUS_MEMBER = 1;
	int APPLICATION_STATUS_MANANGER = 2;
	
	public Response<Group> create(int userid, String name, String detail) {
		int gid = SuperDaoFactory.getGroupDao().insert(DataBuilder.tgroup().name(name).detail(detail).build());
		SuperDaoFactory.getGuDao().insert(DataBuilder.tgu().gid(gid).uid(userid).status(APPLICATION_STATUS_MANANGER).build());
		return new Response<Group>(ServiceConstants.STATUS_INSERT_SUCCESS,DataBuilder.group().id(gid).name(name).build());
	}

	public Response<Group> apply(int userId, int groupId) {
		int result = SuperDaoFactory.getGuDao().insert(DataBuilder.tgu().gid(groupId).uid(userId).status(APPLICATION_STATUS_APPLY).build());
		if(result > 0)
			return new Response<Group>(ServiceConstants.STATUS_INSERT_SUCCESS);
		else if(result == -2)
			return new Response<Group>(ServiceConstants.STATUS_INSERT_DUP);
		else
			return new Response<Group>(ServiceConstants.STATUS_INSERT_FAIL);
	}

	public Response<Group> pass(int userId, int groupId) {
		int result = SuperDaoFactory.getGuDao().update(DataBuilder.tgu().gid(groupId).uid(userId).status(APPLICATION_STATUS_APPLY).build(),
				DataBuilder.tgu().gid(groupId).uid(userId).status(APPLICATION_STATUS_MEMBER).build());
		if(result > 0)
			return new Response<Group>(ServiceConstants.STATUS_UPDATE_SUCCESS);
		else
			return new Response<Group>(ServiceConstants.STATUS_UPDATE_FAIL);
	}

	public Response<List<Group>> getManaging(int userId) {
		List<TableGU> list = SuperDaoFactory.getGuDao().querys(DataBuilder.tgu().uid(userId).status(APPLICATION_STATUS_MANANGER).build());
		List<Group> groups  = new ArrayList<Group>();
		for (TableGU gu : list) {
			groups.add(DataBuilder.group().id(gu.getGroupId()).build());
		}
		return new Response<List<Group>>(ServiceConstants.STATUS_QUERY_SUCCESS,groups);
	}

	public Response<Group> set_mananger(int userId, int groupId) {
		int result = SuperDaoFactory.getGuDao().update(DataBuilder.tgu().gid(groupId).uid(userId).build(),
				DataBuilder.tgu().gid(groupId).uid(userId).status(APPLICATION_STATUS_MANANGER).build());
		if(result > 0)
			return new Response<Group>(ServiceConstants.STATUS_UPDATE_SUCCESS);
		else
			return new Response<Group>(ServiceConstants.STATUS_UPDATE_FAIL);
	}

	public Response<List<User>> getApplicant(int groupId) {
		List<TableGU> list = SuperDaoFactory.getGuDao().querys(DataBuilder.tgu().gid(groupId).status(APPLICATION_STATUS_APPLY).build());
		List<User> users  = new ArrayList<User>();
		for (TableGU gu : list) {
			users.add(new User(SuperDaoFactory.getUserDao().query(DataBuilder.tuser().id(gu.getUserId()).build())));
		}
		return new Response<List<User>>(ServiceConstants.STATUS_QUERY_SUCCESS,users);
	}

	public Response<Group> search(String name) {
		TableGroup group = SuperDaoFactory.getGroupDao().query(DataBuilder.tgroup().name(name).build());
		if(group != null)
			return new Response<Group>(ServiceConstants.STATUS_QUERY_SUCCESS,new Group(group));
		return new Response<Group>(ServiceConstants.STATUS_QUERY_SUCCESS,new Group());
	}

	public Response<Group> detail(int groupId) {
		TableGroup tgroup = SuperDaoFactory.getGroupDao().query(DataBuilder.tgroup().id(groupId).build());
		Group group = new Group(tgroup);
		group.setUsers(new ArrayList<User>());
		List<TableGU> list = SuperDaoFactory.getGuDao().querys(DataBuilder.tgu().gid(group.getId()).build());
		for (TableGU tableGU : list) {
			group.getUsers().add(new User(SuperDaoFactory.getUserDao().query(DataBuilder.tuser().id(tableGU.getUserId()).build())));
		}
		return new Response<Group>(ServiceConstants.STATUS_QUERY_SUCCESS,group);
	}
	
	
}
